package com.ysd.lis.service.bac.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.script.ScriptUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacRule;
import com.ysd.lis.entity.bac.BacRuleCondition;
import com.ysd.lis.entity.bac.BacRuleOperat;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.mapper.bac.BacRuleMapper;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.bac.BacRuleConditionService;
import com.ysd.lis.service.bac.BacRuleOperatService;
import com.ysd.lis.service.bac.BacRuleService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 52771
 * @description 针对表【BAC_RULE(微生物规则表)】的数据库操作Service实现
 * @createDate 2024-08-20 14:49:09
 */
@Service
public class BacRuleServiceImpl extends ServiceImpl<BacRuleMapper, BacRule> implements BacRuleService {

    @Autowired
    BacRuleMapper bacRuleMapper;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Autowired
    BacRuleConditionService bacRuleConditionService;
    @Autowired
    BacRuleOperatService bacRuleOperatService;

    @Override
    public Result findBacRuleList(BacRule bacRule) {
        return Result.succ(1, "查询成功", bacRuleMapper.selectList(new LambdaQueryWrapper<BacRule>()));
    }

    @Override
    public Result findBacRuleConditionOperatList(String code) {
        Map returnMap = new HashMap();
        LambdaQueryWrapper<BacRuleCondition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacRuleCondition::getRuleCode, code).eq(BacRuleCondition::getDelFlag, 0);
        returnMap.put("conditionList", bacRuleConditionService.list(lambdaQueryWrapper));
        LambdaQueryWrapper<BacRuleOperat> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(BacRuleOperat::getRuleCode, code).eq(BacRuleOperat::getDelFlag, 0);
        returnMap.put("operatList", bacRuleOperatService.list(lambdaQueryWrapper1));
        return Result.succ(1, "查询成功", returnMap);
    }

    @Override
    public Result addBacRule(BacRule bacRule) {
        int insert = bacRuleMapper.insert(bacRule);
        if (insert > 0) {
            return Result.succ(1, "新增成功", bacRule);
        } else {
            return Result.succ(400, "新增失败", bacRule);
        }
    }

    @Override
    public Result deleteBacRule(List<String> codes) {
        LambdaQueryWrapper<BacRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(BacRule::getRuleCode, codes);
        int i = bacRuleMapper.delete(lambdaQueryWrapper);

        LambdaQueryWrapper<BacRuleCondition> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(BacRuleCondition::getRuleCode, codes);
        bacRuleConditionService.remove(lambdaQueryWrapper1);

        LambdaQueryWrapper<BacRuleOperat> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.in(BacRuleOperat::getRuleCode, codes);
        bacRuleOperatService.remove(lambdaQueryWrapper2);

        if (i > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.succ(400, "删除失败", "");
        }
    }

    @Override
    public Result updateBacRule(BacRule bacRule) {
        int i = bacRuleMapper.updateById(bacRule);
        if (i > 0) {
            return Result.succ(1, "修改成功", bacRule);
        } else {
            return Result.succ(400, "修改失败", bacRule);
        }
    }

    @Override
    public Result findBacRuleDetialList(BacRule bacRule) {
        Map returnMap = new HashMap();
        List<BacRuleCondition> dataList = new ArrayList<>();
        List<Map<String, Object>> bacRuleDetailList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("BacRuleDetail").getData();
        LambdaQueryWrapper<BacRuleCondition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacRuleCondition::getRuleCode, bacRule.getRuleCode()).eq(BacRuleCondition::getDelFlag, 0);
        lambdaQueryWrapper.orderByAsc(BacRuleCondition::getConditionValue);
        List<BacRuleCondition> list = bacRuleConditionService.list(lambdaQueryWrapper);
        bacRuleDetailList.forEach(a -> {
            Optional<BacRuleCondition> code = list.stream().filter(s -> a.get("code").equals(s.getConditionType())).findFirst();
            if (code.isPresent()) {
                BacRuleCondition bacRuleCondition = code.get();
                bacRuleCondition.setConditionName(a.get("name") + "");
                dataList.add(bacRuleCondition);
            } else {
                BacRuleCondition bacRuleCondition = new BacRuleCondition();
                bacRuleCondition.setConditionType(a.get("code") + "");
                bacRuleCondition.setConditionName(a.get("name") + "");
                dataList.add(bacRuleCondition);
            }
        });

        LambdaQueryWrapper<BacRuleOperat> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(BacRuleOperat::getRuleCode, bacRule.getRuleCode()).eq(BacRuleOperat::getDelFlag, 0);
        returnMap.put("operatList", bacRuleOperatService.list(lambdaQueryWrapper1));
        returnMap.put("conditionList", dataList);

        return Result.succ(1, "查询成功", returnMap);
    }

    @Override
    public Result saveAllRule(BacRule bacRule) {
        bacRuleMapper.updateById(bacRule);
        //先删除后插入
        LambdaQueryWrapper<BacRuleCondition> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacRuleCondition::getRuleCode, bacRule.getRuleCode());
        bacRuleConditionService.remove(lambdaQueryWrapper);

        LambdaQueryWrapper<BacRuleOperat> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(BacRuleOperat::getRuleCode, bacRule.getRuleCode());
        bacRuleOperatService.remove(lambdaQueryWrapper1);

        //插入
        if (CollectionUtils.isNotEmpty(bacRule.getConditionList())) {
            bacRule.getConditionList().forEach(a -> {
                a.setRuleCode(bacRule.getRuleCode());
            });
            bacRuleConditionService.saveBatch(bacRule.getConditionList());
        }
        if (CollectionUtils.isNotEmpty(bacRule.getOperatList())) {
            bacRule.getOperatList().forEach(a -> {
                a.setRuleCode(bacRule.getRuleCode());
            });
            bacRuleOperatService.saveBatch(bacRule.getOperatList());
        }

        return Result.succ(1, "新增成功", "");
    }


    @Override
    public Boolean checkProcessRule(LabMaininfo labMaininfo, String ruleType) {

        Object eval = ScriptUtil.eval("5>6");
        System.out.println(eval);

        LambdaQueryWrapper<BacRuleOperat> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacRuleOperat::getOperatRule, ruleType).eq(BacRuleOperat::getEffect, true).eq(BacRuleOperat::getDelFlag, 0);
        List<BacRuleOperat> list = bacRuleOperatService.list(lambdaQueryWrapper);
        int i = 0;
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> collect = list.stream().map(BacRuleOperat::getRuleCode).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                LambdaQueryWrapper<BacRuleCondition> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.in(BacRuleCondition::getRuleCode, collect).eq(BacRuleCondition::getDelFlag, 0).isNotNull(BacRuleCondition::getConditionValue);
                List<BacRuleCondition> list1 = bacRuleConditionService.list(lambdaQueryWrapper1);
                for (BacRuleCondition bacRuleCondition : list1) {
                    if (StrUtil.isNotEmpty(bacRuleCondition.getConditionValue())) {
                        String[] split = bacRuleCondition.getConditionValue().split(",");
                        switch (bacRuleCondition.getConditionType()) {
                            case "1":
                                if (bacRuleCondition.getMeetWith()) {
                                    if (ToolsUtils.isNotEmpty(labMaininfo.getPatType()) && ListUtil.toList(split).indexOf(labMaininfo.getPatType()) > -1) {
                                        i += 1;
                                    }
                                }
                                continue;
                            case "2":
                                if (bacRuleCondition.getMeetWith()) {
                                    if (ToolsUtils.isNotEmpty(labMaininfo.getSex()) && ListUtil.toList(split).indexOf(labMaininfo.getSex()) > -1) {
                                        i += 1;
                                    }
                                }
                                continue;
                            case "3":
                                for (String s : split) {
                                    if (ToolsUtils.isNotEmpty(labMaininfo.getAge()) && labMaininfo.getAge() > Integer.parseInt(s.split("-")[0]) && labMaininfo.getAge() < Integer.parseInt(s.split("-")[1])) {
                                        i += 1;
                                    }
                                }
                                continue;
                            case "4":
                                if (bacRuleCondition.getMeetWith()) {
                                    if (ListUtil.toList(split).indexOf(labMaininfo.getReqDept()) > -1) {
                                        i += 1;
                                    }
                                }
                                continue;
                        }
                    }
                }
                if (i == list1.size()) {
                    return true;
                }
            }
        }
            return false;
    }

    public static void main(String[] args) {
        Object eval = ScriptUtil.eval("3=='3'&&3<=6");
        System.out.println(eval);
    }
}
